package cn.green.core.components.dialect.impls.mysql.grammar;

import cn.green.core.common.enums.field.ColumnType;
import cn.green.core.common.enums.table.PrimaryKeySet;
import cn.green.core.components.exceptions.QDdlException;

/**
 * @author wang.yishu
 * @version 1.0
 * @className MysqlGrammars
 * @description mysql语法
 * @date 2024/11/15 13:45
 */
public final class MysqlGrammars {

    public static class SYMBOL {
        public static final String BRACKET_LEFT = "(";
        public static final String BRACKET_RIGHT = ")";
        public static final String BLANKET = " ";
        public static final String S_QT_MARK = "'";
        public static final String P = "`";
        public static final String COMMA = ",";
        public static final String SEMICOLON = ";";
        public static final String VALUE_S = "?";
        public static final String ALLOCATE = "=";
        public static final String COLON = ".";
        public static final String S_ET_MARK = "\"";
    }
    public static class CONDITION {
        public static final String WHERE = "where";
        public static final String AND = "and";
        public static final String OR = "or";
        public static final String EQ = "=";
        public static final String NE = "!=";
        public static final String GT = ">";
        public static final String LT = "<";
        public static final String GE = ">=";
        public static final String LE = "<=";
        public static final String IN = "in";
        public static final String LK = "like";
    }
    public static class DDL {
        public static final String TABLE = "table";
        public static final String CREATE = "create";
        public static final String DROP = "drop";
        public static final String ALTER = "alter";
        public static final String ADD = "add";
        public static final String UPD = "update";
        public static final String CHANGE = "change";
        public static final String COLUMN = "column";
        public static final String COMMENT = "comment";
        public static final String PRIMARY_KEY = "primary key";
        public static final String CRT_TABLE_CHARSET = "collate = utf8mb4_bin row_format = DYNAMIC";
    }
    public static class DML {
        public static final String INSERT_INTO = "insert into";
        public static final String VALUE = "values";
        public static final String UPDATE = "update";
        public static final String SET = "set";
    }

    public static class DQL {
        public static final String SELECT = "select";
        public static final String ALL = "*";
        public static final String COUNT = "count(1)";
        public static final String FROM = "from";
        public static final String ORDER_BY = "order by";
        public static final String LIMIT = "limit";
        public static final String PRIMARY_TABLE = "p_table";
        public static final String AS = "as";
        public static final String LEFT_JOIN = "left join";
        public static final String RIGHT_JOIN = "right join";
        public static final String INNER_JOIN = "inner join";
        public static final String JOIN = "join";
        public static final String ON = "on";
    }

    public static class DCL {
        public static final String SELECT = "SELECT";
        public static final String COUNT = "COUNT(1)";
        public static final String FROM = "FROM";
        public static final String INFORMATION_SCHEMA_TABLE = "information_schema.tables";
        public static final String INFORMATION_SCHEMA_COLUMN = "information_schema.columns";
        public static final String TABLE_NAME = "table_name";
        public static final String TABLE_SCHEMA = "table_schema";
        public static final String COLUMN_NAME = "column_name";
    }

    public static class COLUMN_FEATURE {
        public static String PRIMARY_KEY = "primary key ";
    }

    public static class COLUMN_TYPE {
        public static String NUMBER0 = " int ";
        public static String DOUBLE0 = " double ";
        public static String VARCHAR = " varchar ";
        public static String LONGTEXT = " longtext ";
        public static String BLOB = " blob ";
        public static String LONG_BLOB = " longblob ";
        public static String DATE = " timestamp ";
    }

    public static String transform(ColumnType type, Integer length, Integer decimalLength) {
        String rtn;
        switch (type) {
            case NUMBER:
                if (decimalLength == null || decimalLength == 0) {
                    rtn = "int(" + length + ")";
                } else {
                    rtn = "double(" + length + "," + decimalLength + ")";
                }
                break;
            case SHORT_STRING:
                rtn = "varchar(" + length + ")";
                break;
            case LONG_STRING:
                rtn = "longtext";
                break;
            case BOOLEAN:
                rtn = "boolean";
                break;
            case BLOB:
                rtn = "blob";
                break;
            case LONG_BLOB:
                rtn = "longblob";
                break;
            case DATE_TIME:
                rtn = "timestamp";
                break;
            default:
                throw new QDdlException("mysql ddl language executes failure : column type is wrong.");
        }
        return rtn;
    }

    public static String primary(PrimaryKeySet primaryKeySet) {
        switch (primaryKeySet) {
            case AUTO_INCREASE:
                return "auto_increment ";
            case UUID:
            default:
                return "";
        }
    }
}
